/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "MWmi_Win32_LoggedUser.h"

//---------------------------------------------------------------------------

MWmi_Win32_LoggedUser::MWmi_Win32_LoggedUser(void)
{
	propertyCount = 1;
	Initialize();
}

MWmi_Win32_LoggedUser::~MWmi_Win32_LoggedUser(void)
{
	DeleteChild();
}

//---------------------------------------------------------------------------


MWmiInfo* MWmi_Win32_LoggedUser::CreateChild(void)
{
	MWmi_Win32_LoggedUser *elem;

	try{
		elem = new MWmi_Win32_LoggedUser;
	}
	catch(...){
		elem = NULL;
	}
	return elem;
}

void MWmi_Win32_LoggedUser::DeleteChild(void)
{
	MWmi_Win32_LoggedUser *elem;
	
	while(next!=NULL){
		elem = (MWmi_Win32_LoggedUser*)next;
		next = elem->next;
		elem->next = NULL;
		delete elem;
	}
}

//---------------------------------------------------------------------------

void MWmi_Win32_LoggedUser::Initialize(void)
{
	xLoggedUser = _T("");
}

//---------------------------------------------------------------------------

CString MWmi_Win32_LoggedUser::GetQueryString(void)
{
	CString str;

	str = _T("select UserName from Win32_ComputerSystem");
	return str;
}

//---------------------------------------------------------------------------

CString MWmi_Win32_LoggedUser::GetPropertyName(int idxIn)
{
	CString str;

	switch(idxIn){
		case 0 :
			str = _T("UserName");
			break;

		default :
			str = _T("");
			break;
	}

	return str;
}

//---------------------------------------------------------------------------

bool MWmi_Win32_LoggedUser::SetPropertyValue(int idxIn, VARIANT *vtIn)
{
	if(vtIn==NULL) return true;
	if(vtIn->vt==VT_NULL) return true;

	switch(idxIn){
		case 0 :
			if(vtIn->vt!=VT_BSTR) return false;
			xLoggedUser = vtIn->bstrVal;
			break;

		default :
			return false;
	}

	return true;
}

//---------------------------------------------------------------------------

bool MWmi_Win32_LoggedUser::SetFromPointer(MWmiInfo* wmiInfoIn)
{
	MWmi_Win32_LoggedUser *elem;
	MWmi_Win32_LoggedUser *elemNew;

	DeleteChild();
	Initialize();
	if(wmiInfoIn==NULL) return true;

	elem = (MWmi_Win32_LoggedUser*)wmiInfoIn;

	xLoggedUser = elem->xLoggedUser;

	if(elem->next==NULL) return true;

	try{
		elemNew = new MWmi_Win32_LoggedUser;
	}
	catch(...){
		elemNew = NULL;
	}
	if(elemNew==NULL) return false;

	next = elemNew;

	return elemNew->SetFromPointer(elem->next);
}

//---------------------------------------------------------------------------

bool MWmi_Win32_LoggedUser::IsEmpty(void)
{
	return xLoggedUser.IsEmpty() ? true : false;
}

//---------------------------------------------------------------------------
